<?php
/* @var $this PhancongController */

$this->breadcrumbs=array(
	'Phân công'=>array('/phancong/admin'),
	'Xem phân công',
);

$this->menu=array(
	array('label'=>'Phân công giảng dạy','url'=>array('admin')),
	array('label'=>'Xem phân công','url'=>array('xemphancong')),
	array('label'=>'Phân công công tác khác','url'=>array('pccongtackhac/admin')),
	array('label'=>'Quản lý công tác khác','url'=>array('congtackhac/admin')),
	//array('label'=>'Quản lý thời khóa biểu','url'=>array('upload/admin')),
);

Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
	$('.search-form').toggle();
	return false;
});
$('.search-form form').submit(function(){
	$('#xemphancong-grid').yiiGridView('update', {
		data: $(this).serialize()
	});
	return false;
});
");

$toggleUDetails = <<<JS
 $('body').on('click','a.toggle',function(e){
    e.preventDefault();

    if(this.href.split('#')[1]=='loaded') return $(this).closest("tr").next('tr.toggle').toggle();

    trow=$(this).closest("tr");

   var ajaxOpts={type:"POST", url:this.href ,dataType:'json',success:function(data){
            $(trow).after(data.row);
      }
    };

   this.href=this.href+'#loaded';

   $.ajax(ajaxOpts);

  });
JS;
Yii::app()->clientScript->registerScript('toggleUD', $toggleUDetails, CClientScript::POS_READY);
?>
<h1>Thống kê số tiết của từng giảng viên</h1>

<p><?php echo CHtml::link('Bộ lọc tìm kiếm','#',array('class'=>'search-button')); ?></p>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_searchxem',array(
	'model'=>$model,
)); ?>
</div><!-- search-form -->


<?php 
$this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'xemphancong-grid',
	'dataProvider'=>$model->search(),
	//'filter'=>$model,
	'columns'=>array(
		array(
			'class'=>'CButtonColumn',
			'header'=>'Chi tiết',
			'template'=>'{view}',
            'buttons'=>array(
				'view'=>array(
                    'label'=>'Details',                        
                    'url'=>'Yii::app()->controller->createUrl("xemPCGiangvien", array("id"=>$data->magv))',
                    'options'=>array('class'=>'toggle',),
				),
            ),
        ),
		array(
            'name'=>'tengv',
        ),
		array(
			'header'=>'Học kì 1',
			'value'=>function($model){ 
			$connection=Yii::app()->db;   // assuming you have configured a "db" connection
			$command=$connection->createCommand()
			->select('SUM(monhoc.sotietLT+monhoc.sotietTH)')
			->from('nhomlop, monhoc, phancong, giangvien')
			->where('nhomlop.namhoc='.(string)Yii::app()->request->cookies['nh'].' AND nhomlop.hocki=1 AND nhomlop.mamh=monhoc.mamh AND nhomlop.maNhom=phancong.maNhom AND phancong.magvchinh=giangvien.magv AND giangvien.magv='.$model->magv.' GROUP BY giangvien.magv')
			->queryRow();
			return $command['SUM(monhoc.sotietLT+monhoc.sotietTH)']*1;
			},
		),
		array(
			'header'=>'Học kì 2',
			'value'=>function($model){ 
			$connection=Yii::app()->db;   // assuming you have configured a "db" connection
			$command=$connection->createCommand()
			->select('SUM(monhoc.sotietLT+monhoc.sotietTH)')
			->from('nhomlop, monhoc, phancong, giangvien')
			->where('nhomlop.namhoc='.(string)Yii::app()->request->cookies['nh'].' AND nhomlop.hocki=2 AND nhomlop.mamh=monhoc.mamh AND nhomlop.maNhom=phancong.maNhom AND phancong.magvchinh=giangvien.magv AND giangvien.magv='.$model->magv.' GROUP BY giangvien.magv')
			->queryRow();
			return $command['SUM(monhoc.sotietLT+monhoc.sotietTH)']*1;
			},
		),
		array(
			'header'=>'Học kì hè',
			'value'=>function($model){ 
			$connection=Yii::app()->db;   // assuming you have configured a "db" connection
			$command=$connection->createCommand()
			->select('SUM(monhoc.sotietLT+monhoc.sotietTH)')
			->from('nhomlop, monhoc, phancong, giangvien')
			->where('nhomlop.namhoc='.(string)Yii::app()->request->cookies['nh'].' AND nhomlop.hocki=3 AND nhomlop.mamh=monhoc.mamh AND nhomlop.maNhom=phancong.maNhom AND phancong.magvchinh=giangvien.magv AND giangvien.magv='.$model->magv.' GROUP BY giangvien.magv')
			->queryRow();
			return $command['SUM(monhoc.sotietLT+monhoc.sotietTH)']*1;
			},
		),
		array(
			'header'=>'Cả năm',
			'value'=>function($model){ 
			$connection=Yii::app()->db;   // assuming you have configured a "db" connection
			$command=$connection->createCommand()
			->select('SUM(monhoc.sotietLT+monhoc.sotietTH)')
			->from('nhomlop, monhoc, phancong, giangvien')
			->where('nhomlop.namhoc='.(string)Yii::app()->request->cookies['nh'].' AND nhomlop.mamh=monhoc.mamh AND nhomlop.maNhom=phancong.maNhom AND phancong.magvchinh=giangvien.magv AND giangvien.magv='.$model->magv.' GROUP BY giangvien.magv')
			->queryRow();
			return $command['SUM(monhoc.sotietLT+monhoc.sotietTH)']*1;
			},
		),
		array(
            'name'=>'loaihinh',
			'value'=>function($model){
				if($model->loaihinh=='gvch')
					return 'Giảng viên cơ hữu';
				else if($model->loaihinh=='gvmg')
					return 'Giảng viên mời giảng';
				else if($model->loaihinh=='gvtgtt')
					return 'Giảng viên thỉnh giảng trong trường';
				else if($model->loaihinh=='gvtgnt')
					return 'Giảng viên thỉnh giảng ngoài trường';
			},
        ),
	),
));
?>